  /************************
 ***   Image3Tool.c   ***
 ************************/
#include	<string.h>

#include	"ImageType/ImageType.h"




image_type *
image1_bluring( image_type *sim, int n, image_type *im )
{
image_type *tim;

	tim  = image1_bluring_x( sim, n );

	im  = image1_bluring_y( tim, n, im );

	image_destroy( tim, 1 );
	return( im );
}


image_type *
image1_bluring_x( image_type *sim, int N )
{
u_char	*sp0, *tp, *sp2;
int	i,	j, sum, k;
image_type * im;


	im = image_create( sim->row, sim->column, 1, 1, NULL );

	for( i = 0 ; i < im->row ; i++ ){
		sp0 = IMAGE_PIXEL( sim, i, 0 );
		for( j = 0, sum = 0; j < (2*N + 1); j++, sp0++){
			sum += *sp0;
		}

		k = N;
		sp2 = sp0 -(2*N + 1);
		tp = IMAGE_PIXEL( im, i, N);
		*tp = sum/(2*N + 1); 
		tp++;

		for( ; j < im->column ; j++, sp0++, sp2++, tp++){
			sum += (*sp0 - *sp2);
			*tp = sum/(2*N + 1); 
		}

		sp0 = IMAGE_PIXEL( sim, i, sim->column - N );
		for( k = 0; k < N ; k++, tp++, sp0++ ){
			*tp = *sp0;
		}

		sp0 = IMAGE_PIXEL( sim, i, 0 );
		tp = IMAGE_PIXEL( im, i, 0 );
		for( j = 0; j < N; j++, sp0++, tp++){
			*tp = *sp0;
		}

	}
	return( im );
}



/*
image_type *
image1_bluring_x( image_type *sim, int thresh )
{
//thresh = 5;
u_char	*sp0, *sp1, *sp2;
int	i,	j, sum, k;
image_type * im = image_create( sim->row, sim->column, 1, 1, NULL );

	for( i = 0 ; i < im->row ; i++ ){
		for( j = 0, sum = 0; j < (2*thresh + 1); j++){
			sp0 = IMAGE_PIXEL( sim, i, j );
			sum += *sp0;
		}

		k = thresh;
		sp1 = IMAGE_PIXEL( im, i, k++ );
		*sp1 = sum/(2*thresh + 1); 

		for( ; j < im->column ; j++ ){
			sp0 = IMAGE_PIXEL( sim, i, j );
			sp2 = IMAGE_PIXEL( sim, i, j-(2*thresh + 1) );
			sum += *sp0 - *sp2;
			sp1 = IMAGE_PIXEL( im, i, k++ );
			*sp1 = sum/(2*thresh + 1); 
		}

		for( ; k < im->column ; k++ ){
			sp0 = IMAGE_PIXEL( sim, i, k );
			sp1 = IMAGE_PIXEL( im, i, k );
			*sp1 = *sp0;
		}
		for( j = 0; j < thresh; j++){
			sp0 = IMAGE_PIXEL( sim, i, j );
			sp1 = IMAGE_PIXEL( im, i, j );
			*sp1 = *sp0;
		}

	}
	return( im );
}
*/


image_type *
image1_bluring_y( image_type *sim, int thresh, image_type *im )
{
u_char	*sp0, *sp1, *sp2;
int	i,	j, sum, k;

int pwidth = sim->column;

	im = image_recreate( im, sim->row, sim->column, 1, 1 );
	for( i = 0 ; i < im->column ; i++ ){
		sp0 = IMAGE_PIXEL( sim, 0, i );
		for( j = 0, sum = 0; j < (2*thresh + 1); j++, sp0+=pwidth){
			sum += *sp0;
		}

		k = thresh;
		sp2 = sp0 - (2*thresh + 1)*pwidth;
		sp1 = IMAGE_PIXEL( im, thresh ,i);
		*sp1 = sum/(2*thresh + 1); 
		sp1 += pwidth;

		for( ; j < im->row ; j++,  sp0+=pwidth, sp2+=pwidth, sp1+=pwidth){
			sum += *sp0 - *sp2;
			*sp1 = sum/(2*thresh + 1); 
		}

		sp0 = IMAGE_PIXEL( sim, sim->row - thresh, i );
		for( k = 0; k < thresh ; k++, sp0+=pwidth, sp1+=pwidth){
			*sp1 = *sp0;
		}

		sp0 = IMAGE_PIXEL( sim, 0, i );
		sp1 = IMAGE_PIXEL( im, 0, i );
		for( j = 0; j < thresh; j++, sp0+=pwidth, sp1+=pwidth){
			*sp1 = *sp0;
		}

	}
	return( im );
}


image_type *
image1_bluring_x_bw( image_type *sim, int thresh )
{
	//thresh = 5;
	u_char	*sp0, *sp1, *sp2;
	int	i,	j, sum, k, count;
	image_type * im = image_create( sim->row, sim->column, 1, 1, NULL );

	for( i = 0 ; i < im->row ; i++ ){
		for( j = 0, sum = 0, count = 0; j < (2*thresh + 1); j++){
			sp0 = IMAGE_PIXEL( sim, i, j );
			sum += *sp0;
			if((*sp0)!=0){
				count++;
			}
		}

		k = thresh;
		sp1 = IMAGE_PIXEL( im, i, k++ );
		*sp1 = sum/(2*thresh + 1); 

		for( ; j < im->column ; j++ ){
			sp0 = IMAGE_PIXEL( sim, i, j );
			sp2 = IMAGE_PIXEL( sim, i, j-(2*thresh + 1) );
			sum += *sp0 - *sp2;
			sp1 = IMAGE_PIXEL( im, i, k++ ); 
			if((*sp0)!=0){
				count++;
			}

			if((*sp2)!=0){
				count--;
			}

			if(sum!=0){
				*sp1 = sum/count;
			}else{
				*sp1 = 0;
			}
		}

		for( ; k < im->column ; k++ ){
			//sp0 = IMAGE_PIXEL( sim, i, k );
			sp1 = IMAGE_PIXEL( im, i, k );
			*sp1 = 0;
		}
		for( j = 0; j < thresh; j++){
			//sp0 = IMAGE_PIXEL( sim, i, j );
			sp1 = IMAGE_PIXEL( im, i, j );
			*sp1 = 0;
		}

	}
	return( im );
}

image_type *
image1_bluring_y_bw( image_type *sim, int thresh )
{
	u_char	*sp0, *sp1, *sp2;
	int	i,	j, sum, k, count;
	image_type * im = image_create( sim->row, sim->column, 1, 1, NULL );

	for( i = 0 ; i < im->column ; i++ ){
		for( j = 0, sum = 0, count = 0; j < (2*thresh + 1); j++){
			sp0 = IMAGE_PIXEL( sim, j, i );
			sum += *sp0;
			if((*sp0)!=0){
				count++;
			}
		}

		k = thresh;
		sp1 = IMAGE_PIXEL( im, k++ ,i);
		*sp1 = sum/(2*thresh + 1); 

		for( ; j < im->row ; j++ ){
			sp0 = IMAGE_PIXEL( sim, j, i );
			sp2 = IMAGE_PIXEL( sim, j-(2*thresh + 1), i );
			sum += *sp0 - *sp2;
			sp1 = IMAGE_PIXEL( im, k++ ,i);
			if((*sp0)!=0){
				count++;
			}

			if((*sp2)!=0){
				count--;
			}

			if(sum!=0){
				*sp1 = sum/count; 
			}else{
				*sp1 = 0;
			}
		}

		for( ; k < im->row ; k++ ){
			//sp0 = IMAGE_PIXEL( sim, k, i );
			sp1 = IMAGE_PIXEL( im, k, i );
			*sp1 = 0;
		}

		for( j = 0; j < thresh; j++){
			//sp0 = IMAGE_PIXEL( sim, j, i );
			sp1 = IMAGE_PIXEL( im, j, i );
			*sp1 = 0;
		}

	}
	return( im );
}